package com.sodyu.elasticsearch.dal.dao;

import com.sodyu.elasticsearch.dal.entity.Dianpu;
import com.sodyu.elasticsearch.dal.entity.DianpuExample;
import com.sodyu.elasticsearch.dal.entity.DianpuExample.Criteria;
import com.sodyu.elasticsearch.dal.entity.DianpuExample.Criterion;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.*;
public class DianpuSqlProvider {

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dianpu
     *
     * @mbggenerated Fri Sep 15 19:01:58 CST 2017
     */
    public String countByExample(DianpuExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("dianpu");
        applyWhere(example, false);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dianpu
     *
     * @mbggenerated Fri Sep 15 19:01:58 CST 2017
     */
    public String deleteByExample(DianpuExample example) {
        BEGIN();
        DELETE_FROM("dianpu");
        applyWhere(example, false);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dianpu
     *
     * @mbggenerated Fri Sep 15 19:01:58 CST 2017
     */
    public String insertSelective(Dianpu record) {
        BEGIN();
        INSERT_INTO("dianpu");

        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=BIGINT}");
        }

        if (record.getUid() != null) {
            VALUES("uid", "#{uid,jdbcType=VARCHAR}");
        }

        if (record.getShopId() != null) {
            VALUES("shopId", "#{shopId,jdbcType=VARCHAR}");
        }

        if (record.getShopName() != null) {
            VALUES("shopName", "#{shopName,jdbcType=VARCHAR}");
        }

        if (record.getSaleCount() != null) {
            VALUES("saleCount", "#{saleCount,jdbcType=VARCHAR}");
        }

        if (record.getProductCount() != null) {
            VALUES("productCount", "#{productCount,jdbcType=VARCHAR}");
        }

        if (record.getShopUrl() != null) {
            VALUES("shopUrl", "#{shopUrl,jdbcType=VARCHAR}");
        }

        if (record.getShopRealUrl() != null) {
            VALUES("shopRealUrl", "#{shopRealUrl,jdbcType=VARCHAR}");
        }

        if (record.getGoodratePercent() != null) {
            VALUES("goodratePercent", "#{goodratePercent,jdbcType=VARCHAR}");
        }

        if (record.getMainAuction() != null) {
            VALUES("mainAuction", "#{mainAuction,jdbcType=VARCHAR}");
        }

        if (record.getLocation() != null) {
            VALUES("location", "#{location,jdbcType=VARCHAR}");
        }

        if (record.getTelephone() != null) {
            VALUES("telephone", "#{telephone,jdbcType=VARCHAR}");
        }

        if (record.getType() != null) {
            VALUES("type", "#{type,jdbcType=VARCHAR}");
        }

        if (record.getCategory() != null) {
            VALUES("category", "#{category,jdbcType=VARCHAR}");
        }

        if (record.getDestination() != null) {
            VALUES("destination", "#{destination,jdbcType=VARCHAR}");
        }

        if (record.getAllkeys() != null) {
            VALUES("allkeys", "#{allkeys,jdbcType=VARCHAR}");
        }

        if (record.getDataChange_CreateUser() != null) {
            VALUES("DataChange_CreateUser", "#{dataChange_CreateUser,jdbcType=VARCHAR}");
        }

        if (record.getDataChange_CreateTime() != null) {
            VALUES("DataChange_CreateTime", "#{dataChange_CreateTime,jdbcType=TIMESTAMP}");
        }

        if (record.getDataChange_LastUser() != null) {
            VALUES("DataChange_LastUser", "#{dataChange_LastUser,jdbcType=VARCHAR}");
        }

        if (record.getDataChange_LastTime() != null) {
            VALUES("DataChange_LastTime", "#{dataChange_LastTime,jdbcType=TIMESTAMP}");
        }

        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dianpu
     *
     * @mbggenerated Fri Sep 15 19:01:58 CST 2017
     */
    public String selectByExample(DianpuExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("uid");
        SELECT("shopId");
        SELECT("shopName");
        SELECT("saleCount");
        SELECT("productCount");
        SELECT("shopUrl");
        SELECT("shopRealUrl");
        SELECT("goodratePercent");
        SELECT("mainAuction");
        SELECT("location");
        SELECT("telephone");
        SELECT("type");
        SELECT("category");
        SELECT("destination");
        SELECT("allkeys");
        SELECT("DataChange_CreateUser");
        SELECT("DataChange_CreateTime");
        SELECT("DataChange_LastUser");
        SELECT("DataChange_LastTime");
        FROM("dianpu");
        applyWhere(example, false);

        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        StringBuilder sql = new StringBuilder(SQL());

        if (example.getPage() > -1 && example.getPageSize() > -1) {
            sql.append(" limit ").append(example.getPage()).append(" , ").append(example.getPageSize());
        } else if (example.getPageSize() > -1) {
            sql.append(" limit ").append(example.getPageSize());
        }
        return sql.toString();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dianpu
     *
     * @mbggenerated Fri Sep 15 19:01:58 CST 2017
     */
    public String updateByExampleSelective(Map<String, Object> parameter) {
        Dianpu record = (Dianpu) parameter.get("record");
        DianpuExample example = (DianpuExample) parameter.get("example");

        BEGIN();
        UPDATE("dianpu");

        if (record.getId() != null) {
            SET("id = #{record.id,jdbcType=BIGINT}");
        }

        if (record.getUid() != null) {
            SET("uid = #{record.uid,jdbcType=VARCHAR}");
        }

        if (record.getShopId() != null) {
            SET("shopId = #{record.shopId,jdbcType=VARCHAR}");
        }

        if (record.getShopName() != null) {
            SET("shopName = #{record.shopName,jdbcType=VARCHAR}");
        }

        if (record.getSaleCount() != null) {
            SET("saleCount = #{record.saleCount,jdbcType=VARCHAR}");
        }

        if (record.getProductCount() != null) {
            SET("productCount = #{record.productCount,jdbcType=VARCHAR}");
        }

        if (record.getShopUrl() != null) {
            SET("shopUrl = #{record.shopUrl,jdbcType=VARCHAR}");
        }

        if (record.getShopRealUrl() != null) {
            SET("shopRealUrl = #{record.shopRealUrl,jdbcType=VARCHAR}");
        }

        if (record.getGoodratePercent() != null) {
            SET("goodratePercent = #{record.goodratePercent,jdbcType=VARCHAR}");
        }

        if (record.getMainAuction() != null) {
            SET("mainAuction = #{record.mainAuction,jdbcType=VARCHAR}");
        }

        if (record.getLocation() != null) {
            SET("location = #{record.location,jdbcType=VARCHAR}");
        }

        if (record.getTelephone() != null) {
            SET("telephone = #{record.telephone,jdbcType=VARCHAR}");
        }

        if (record.getType() != null) {
            SET("type = #{record.type,jdbcType=VARCHAR}");
        }

        if (record.getCategory() != null) {
            SET("category = #{record.category,jdbcType=VARCHAR}");
        }

        if (record.getDestination() != null) {
            SET("destination = #{record.destination,jdbcType=VARCHAR}");
        }

        if (record.getAllkeys() != null) {
            SET("allkeys = #{record.allkeys,jdbcType=VARCHAR}");
        }

        if (record.getDataChange_CreateUser() != null) {
            SET("DataChange_CreateUser = #{record.dataChange_CreateUser,jdbcType=VARCHAR}");
        }

        if (record.getDataChange_CreateTime() != null) {
            SET("DataChange_CreateTime = #{record.dataChange_CreateTime,jdbcType=TIMESTAMP}");
        }

        if (record.getDataChange_LastUser() != null) {
            SET("DataChange_LastUser = #{record.dataChange_LastUser,jdbcType=VARCHAR}");
        }

        if (record.getDataChange_LastTime() != null) {
            SET("DataChange_LastTime = #{record.dataChange_LastTime,jdbcType=TIMESTAMP}");
        }

        applyWhere(example, true);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dianpu
     *
     * @mbggenerated Fri Sep 15 19:01:58 CST 2017
     */
    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("dianpu");

        SET("id = #{record.id,jdbcType=BIGINT}");
        SET("uid = #{record.uid,jdbcType=VARCHAR}");
        SET("shopId = #{record.shopId,jdbcType=VARCHAR}");
        SET("shopName = #{record.shopName,jdbcType=VARCHAR}");
        SET("saleCount = #{record.saleCount,jdbcType=VARCHAR}");
        SET("productCount = #{record.productCount,jdbcType=VARCHAR}");
        SET("shopUrl = #{record.shopUrl,jdbcType=VARCHAR}");
        SET("shopRealUrl = #{record.shopRealUrl,jdbcType=VARCHAR}");
        SET("goodratePercent = #{record.goodratePercent,jdbcType=VARCHAR}");
        SET("mainAuction = #{record.mainAuction,jdbcType=VARCHAR}");
        SET("location = #{record.location,jdbcType=VARCHAR}");
        SET("telephone = #{record.telephone,jdbcType=VARCHAR}");
        SET("type = #{record.type,jdbcType=VARCHAR}");
        SET("category = #{record.category,jdbcType=VARCHAR}");
        SET("destination = #{record.destination,jdbcType=VARCHAR}");
        SET("allkeys = #{record.allkeys,jdbcType=VARCHAR}");
        SET("DataChange_CreateUser = #{record.dataChange_CreateUser,jdbcType=VARCHAR}");
        SET("DataChange_CreateTime = #{record.dataChange_CreateTime,jdbcType=TIMESTAMP}");
        SET("DataChange_LastUser = #{record.dataChange_LastUser,jdbcType=VARCHAR}");
        SET("DataChange_LastTime = #{record.dataChange_LastTime,jdbcType=TIMESTAMP}");

        DianpuExample example = (DianpuExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dianpu
     *
     * @mbggenerated Fri Sep 15 19:01:58 CST 2017
     */
    public String updateByPrimaryKeySelective(Dianpu record) {
        BEGIN();
        UPDATE("dianpu");

        if (record.getUid() != null) {
            SET("uid = #{uid,jdbcType=VARCHAR}");
        }

        if (record.getShopId() != null) {
            SET("shopId = #{shopId,jdbcType=VARCHAR}");
        }

        if (record.getShopName() != null) {
            SET("shopName = #{shopName,jdbcType=VARCHAR}");
        }

        if (record.getSaleCount() != null) {
            SET("saleCount = #{saleCount,jdbcType=VARCHAR}");
        }

        if (record.getProductCount() != null) {
            SET("productCount = #{productCount,jdbcType=VARCHAR}");
        }

        if (record.getShopUrl() != null) {
            SET("shopUrl = #{shopUrl,jdbcType=VARCHAR}");
        }

        if (record.getShopRealUrl() != null) {
            SET("shopRealUrl = #{shopRealUrl,jdbcType=VARCHAR}");
        }

        if (record.getGoodratePercent() != null) {
            SET("goodratePercent = #{goodratePercent,jdbcType=VARCHAR}");
        }

        if (record.getMainAuction() != null) {
            SET("mainAuction = #{mainAuction,jdbcType=VARCHAR}");
        }

        if (record.getLocation() != null) {
            SET("location = #{location,jdbcType=VARCHAR}");
        }

        if (record.getTelephone() != null) {
            SET("telephone = #{telephone,jdbcType=VARCHAR}");
        }

        if (record.getType() != null) {
            SET("type = #{type,jdbcType=VARCHAR}");
        }

        if (record.getCategory() != null) {
            SET("category = #{category,jdbcType=VARCHAR}");
        }

        if (record.getDestination() != null) {
            SET("destination = #{destination,jdbcType=VARCHAR}");
        }

        if (record.getAllkeys() != null) {
            SET("allkeys = #{allkeys,jdbcType=VARCHAR}");
        }

        if (record.getDataChange_CreateUser() != null) {
            SET("DataChange_CreateUser = #{dataChange_CreateUser,jdbcType=VARCHAR}");
        }

        if (record.getDataChange_CreateTime() != null) {
            SET("DataChange_CreateTime = #{dataChange_CreateTime,jdbcType=TIMESTAMP}");
        }

        if (record.getDataChange_LastUser() != null) {
            SET("DataChange_LastUser = #{dataChange_LastUser,jdbcType=VARCHAR}");
        }

        if (record.getDataChange_LastTime() != null) {
            SET("DataChange_LastTime = #{dataChange_LastTime,jdbcType=TIMESTAMP}");
        }

        WHERE("id = #{id,jdbcType=BIGINT}");

        return SQL();
    }

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table dianpu
     *
     * @mbggenerated Fri Sep 15 19:01:58 CST 2017
     */
    protected void applyWhere(DianpuExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }

        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }

        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }

                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }

                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }

        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}